package com.beardedcoder.lowPower.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.List;

import com.beardedcoder.lowPower.app.AppSettings;
import com.beardedcoder.utils.UtilObjectSerialiser;

import android.content.Context;
import android.content.Intent;

public class TemporaryDataStorage {
	private List<BatteryData> batteryData;
	private static final String FILE_BATTERY_TMP_STORAGE = "FILE_BATTERY_TMP_STORAGE";
	
	
	public TemporaryDataStorage () {
		batteryData = new ArrayList<BatteryData>();
	}
	
	public void outputAlternateToPath (Context context, String path) {
		
		FileInputStream fileInput = getFileInput(context, FILE_BATTERY_TMP_STORAGE);
		FileOutputStream fileOutput = getFileOutput(path);
		try {
			Object data = UtilObjectSerialiser.read(fileInput);
			UtilObjectSerialiser.write(data, fileOutput);
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (OptionalDataException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param batteryIntent
	 */
	public void addBatteryData(Intent batteryIntent, Context context) {
//		batteryData.add(new BatteryData(batteryIntent));
//		if (batteryData.size() >= 25) {
//			save(context);
//		}
	}
	
	public void wipeBatteryStoredData(Context context) {
		context.deleteFile(FILE_BATTERY_TMP_STORAGE);
	}

	/**
	 * Currently this will just flush to file storage (flush)
	 * This will persist the data into the database when that is set up
	 * As the file may already have data in it, we need to read from the file
	 * then add this local object to the object returned. Then save. 
	 */
	@SuppressWarnings("unchecked")
	public void save(Context context) {
		 FileOutputStream file = getFileOutput (context, FILE_BATTERY_TMP_STORAGE);
		 FileInputStream fileInput = getFileInput(context, FILE_BATTERY_TMP_STORAGE);
		
		Object objFileRead = null;
		List<BatteryData> toWrite = new ArrayList<BatteryData>();
		
		try {
			objFileRead = UtilObjectSerialiser.read(fileInput);
			fileInput.close();
		} catch (Exception e) {
			e.printStackTrace();
			AppSettings.log(this.getClass(), "save", "exception stack trace printed: failed to input data to file");
		}
		
		if (objFileRead != null) {
			List<BatteryData> data = (List<BatteryData>) objFileRead;
			if (data.size() >= 1) {
				toWrite = data;
			}
		}
		
		toWrite.addAll(batteryData);
		
		try {
			UtilObjectSerialiser.write(toWrite, file);
		} catch (IOException e) {
			e.printStackTrace();
			AppSettings.log(this.getClass(), "save", "failed to write out");
		}
	}
	
	/**
	 * This will open the persisted file and see how many objects are stored
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public long getPersistedDataSize(Context context) {
		 FileInputStream file = getFileInput(context, FILE_BATTERY_TMP_STORAGE);
		Object oRead = null;
		try {
			oRead = UtilObjectSerialiser.read(file);
		} catch (Exception e) {
			e.printStackTrace();
			AppSettings.log(this.getClass(), "getPersistedDataSize", "exception stack trace printed: failed to read file for input");
		}
		if (oRead == null) {
			return 0;
		} else {
			List<BatteryData> data = (List<BatteryData>) oRead;
			return data.size();
		}
	}
	
	private FileOutputStream getFileOutput (String path) {
		FileOutputStream file = null;
		File f = null;
		
		try {
			f = new File(path);
			file = new FileOutputStream(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		return file;
	}
	
	private FileOutputStream getFileOutput (Context context, String path) {
		FileOutputStream file = null;
		try {		
			file = context.openFileOutput(path, Context.MODE_PRIVATE);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		return file;
	}
	
	private FileInputStream getFileInput(Context context, String path) {
		FileInputStream file = null;
		try {
			file = context.openFileInput(path);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return file;
	}	
	
}
